/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Presentation;

import Business.CategoryBusiness;
import Business.VendorBusiness;
import Objects.Category;
import Objects.Vendor;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Toolkit;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author Nguyen Thanh Ha
 */
public class CategoryDetail extends javax.swing.JDialog {

    /**
     * Creates new form EmployeeManagement
     */
    public CategoryDetail(Frame owner, String title, boolean modal) {
        super(owner, title, modal);
        initComponents();
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation(d.width / 2 - this.getSize().width / 2, (d.height - 40) / 2 - this.getSize().height / 2);
        try {

            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            Logger.getLogger(EmployeeDetail.class.getName()).log(Level.SEVERE, null, ex);
        }
        loadData();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        btnRS_CateDetail = new javax.swing.JButton();
        btnSave_CateDetail = new javax.swing.JButton();
        btnDelete_CateDetail = new javax.swing.JButton();
        btnClose_CateDetail = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtCategoryID_Detail = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtCategoryName_Detail = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        cmbVendor_Detail = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblCategoryDetail = new javax.swing.JTable();

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        btnRS_CateDetail.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Presentation/image/IconReset.png"))); // NOI18N
        btnRS_CateDetail.setText("Reset");
        jToolBar1.add(btnRS_CateDetail);

        btnSave_CateDetail.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Presentation/image/IconSave.png"))); // NOI18N
        btnSave_CateDetail.setText("Save");
        jToolBar1.add(btnSave_CateDetail);

        btnDelete_CateDetail.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Presentation/image/IconDelete.png"))); // NOI18N
        btnDelete_CateDetail.setText("Delete");
        btnDelete_CateDetail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDelete_CateDetailActionPerformed(evt);
            }
        });
        jToolBar1.add(btnDelete_CateDetail);

        btnClose_CateDetail.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Presentation/image/IconClose.png"))); // NOI18N
        btnClose_CateDetail.setText("Close");
        jToolBar1.add(btnClose_CateDetail);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Category"));

        jLabel1.setText("Category ID");

        jLabel2.setText("Category Name");

        jLabel3.setText("Vendor");

        cmbVendor_Detail.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        tblCategoryDetail.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblCategoryDetail);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(57, 57, 57)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(114, 114, 114)
                                .addComponent(cmbVendor_Detail, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(txtCategoryName_Detail, javax.swing.GroupLayout.PREFERRED_SIZE, 352, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(114, 114, 114)
                                    .addComponent(txtCategoryID_Detail, javax.swing.GroupLayout.PREFERRED_SIZE, 352, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 990, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(49, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCategoryID_Detail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(38, 38, 38)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCategoryName_Detail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(34, 34, 34)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbVendor_Detail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 64, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(22, 22, 22))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 472, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(79, 79, 79)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(76, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnDelete_CateDetailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDelete_CateDetailActionPerformed
        // TODO add your handling code here:
        int rs = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete this category?", "Notification", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if (rs == 0) {
            int kt = CategoryBusiness.deleteCategory(txtCategoryID_Detail.getText().trim());
            if (kt > 0) {
                JOptionPane.showMessageDialog(null, "Delete Successful", null, JOptionPane.INFORMATION_MESSAGE);
                this.dispose();
            } else {
                JOptionPane.showMessageDialog(null, "Delete Not Successful", null, JOptionPane.WARNING_MESSAGE);
            }
    }//GEN-LAST:event_btnDelete_CateDetailActionPerformed
    }

    /**
     * this method used to validate components in this form
     *
     * @return variable has String type
     */
    private String validateForm() {
        StringBuilder err = new StringBuilder();

        if (txtCategoryName_Detail.getText().isEmpty()) {
            err.append("\n Category Name is invalid");

        }
        return err.toString();
    }

    /**
     * this method used to load data for combo box in this form
     */
    private void loadData() {
        ArrayList<Vendor> list;
        try {
            list = VendorBusiness.getAllVendor();
            for (int i = 0; i < list.size(); i++) {
                cmbVendor_Detail.addItem(list.get(i).getVendorID());
            }
        } catch (SQLException ex) {
            Logger.getLogger(CategoryDetail.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     * this method used to receiver object category from another form ,then set
     * data for components in this form
     *
     * @param objectCategory
     */
    public void receiverCategory(Category cate) {
        txtCategoryID_Detail.setText(cate.getCategoryID());
        txtCategoryID_Detail.setEnabled(false);
        txtCategoryName_Detail.setText(cate.getCategoryName());

        ArrayList<Vendor> listven = VendorBusiness.getVendorbyID(cate.getVendorID());
        for (int i = 0; i < listven.size(); i++) {
            cmbVendor_Detail.setSelectedItem(listven.get(i).getVendorName());

        }

    }

    /**
     * this method used to update Category Information
     */
    private void updateCategory() {
        String err = validateForm();
        if (err.isEmpty()) {
            String cateID = txtCategoryID_Detail.getText().trim();
            String cateName = txtCategoryName_Detail.getText().trim();

            String venName = cmbVendor_Detail.getSelectedItem().toString();
            ArrayList<Vendor> ListVen = VendorBusiness.getVendorByName(venName);
            for (int i = 0; i < ListVen.size(); i++) {
                
            }
            String venID = ListVen.get(1).getVendorID();

            int rs = CategoryBusiness.updateCategory(cateID, cateName, venID);
            if (rs > 0) {
                JOptionPane.showMessageDialog(null, "Update Successful", null, JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(null, "Update Not Successful", null, JOptionPane.WARNING_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(null, err, "Notification", JOptionPane.WARNING_MESSAGE);
        }
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClose_CateDetail;
    private javax.swing.JButton btnDelete_CateDetail;
    private javax.swing.JButton btnRS_CateDetail;
    private javax.swing.JButton btnSave_CateDetail;
    private javax.swing.JComboBox cmbVendor_Detail;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTable tblCategoryDetail;
    private javax.swing.JTextField txtCategoryID_Detail;
    private javax.swing.JTextField txtCategoryName_Detail;
    // End of variables declaration//GEN-END:variables
}
